	// 注意：crypto-js，js-base64 需要通过 npm install 进行安装
    import crypto from 'crypto-js';
	import { Base64 } from 'js-base64';
	import http from "@/api/request.js"; //引入luch-request封装的请求拦截，响应拦截
	/**
	 * 1.  获取阿里云签名信息
	 */
	function getAliOssInfo() {
		// return http.postRequest('api/oss/temporaryAccessCredentials') //接口由后台提供
		return new Promise((resolve, reject) => {
			uni.request({
				url: 'https://jzxt.jiugezixun.cn/miniprogram/oss/getTemporaryAccessCredentials',
				data: {},
				method: 'GET',
				header: {
					'content-type': 'application/json'
				},
				success: res => {
					if (res.statusCode != 200) {
						uni.showModal({
							title: '错误警告',
							content: `出现${res.statusCode}错误`,
							showCancel: false
						})
						return
					}
					uni.hideLoading();
					resolve(res)
		
				},
				fail: res => {
					uni.hideLoading();
					return reject(res)
				}
			})
		})
	}
	
	// 计算签名。
	function computeSignature(accessKeySecret, canonicalString) {
	  return crypto.enc.Base64.stringify(crypto.HmacSHA1(canonicalString, accessKeySecret));
	}
	
	const date = new Date();
	date.setHours(date.getHours() + 1);
	const policyText = {
	  expiration: date.toISOString(), // 设置policy过期时间。
	  conditions: [
	    // 限制上传大小。
	    ["content-length-range", 0, 1024 * 1024 * 1024],
	  ],
	};
	
	export const getFormDataParams=async()=>{
	  const  {data:res}= await getAliOssInfo()
	  if(res.code==1){
		let credentials=res.data
		const policy = Base64.encode(JSON.stringify(policyText)) // policy必须为base64的string。
		const signature = computeSignature(credentials.accessKeySecret, policy)
		const formData = {
			// OSSAccessKeyId: credentials.AccessKeyId,
			OSSAccessKeyId: credentials.accessKeyId,
			signature,
			policy,
			'x-oss-security-token': credentials.securityToken 
		}
		return formData
	  }else{
		console.log(res.msg)
	  }
	 
	 
	}